package cc.jorange.common.helper;

import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Joiner;

/**
 * json工具类
 * 
 * @author Aaron
 */
public class JsonHelper {
	
	static final Logger logger = LoggerFactory.getLogger(JsonHelper.class);

	public static final Map<String, Object> encodeJson2Map(String json) {
		Map<String, Object> map = null;
		if (StringUtils.isNotEmpty(json)) {
			ObjectMapper mapper = new ObjectMapper();
			try {
				map = mapper.readValue(json, Map.class);
			} catch (JsonParseException e) {
				e.printStackTrace();
				return map;
			} catch (JsonMappingException e) {
				e.printStackTrace();
				return map;
			} catch (IOException e) {
				e.printStackTrace();
				return map;
			}
		}
		return map;
	}
	
	public static <T> T jsonToBean(String json, Class<T> clazz) {
		ObjectMapper jsonToBean = new ObjectMapper();
		jsonToBean.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		try {
			return jsonToBean.readValue(json, clazz);
		} catch (Exception e) {
			logger.error("jsonTobean转换异常", e);
		} 
		return null;
	}

	public static final Map<String, String> encodeJson2Map2String(String json) {
		Map<String, String> map = null;
		if (StringUtils.isNotEmpty(json)) {
			ObjectMapper mapper = new ObjectMapper();
			try {
				map = mapper.readValue(json, Map.class);
			} catch (Exception e) {
				logger.error("jsonToMap转换异常", e);
			}
		}
		return map;
	}

	public static final String encodeMap2Json(Object object) {
		String reslut = null;
		try {
			ObjectMapper objectMapper = new ObjectMapper();
			objectMapper.setSerializationInclusion(Include.NON_NULL);
			reslut = objectMapper.writeValueAsString(object);
		} catch (IOException e) {
			logger.error("ObjectToJson转换异常", e);
		}
		return reslut;
	}

	public static String rawParams(Map<String, String> rawParams) {
		StringBuilder params = new StringBuilder();
		Set<Entry<String, String>> entrySet = rawParams.entrySet();
		if (entrySet.size() > 0)
			params.append("?");
		for (Map.Entry<String, String> entry : entrySet) {
			params.append(entry.getKey());
			params.append("=");
			params.append(entry.getValue());
			params.append("&");
		}
		return params.toString();
	}

	public static String collectionToString(Collection<String> stringCollection) {
		return Joiner.on(",").join(stringCollection);
	}
	
	
	
//	public static final String encodeMap2Json(Map<String, String> dto) {
//	try {
//		ObjectMapper objectMapper = new ObjectMapper();
//		return objectMapper.writeValueAsString(dto);
//	}
//	catch (IOException e) {
//		e.printStackTrace();
//		return null;
//	}
//}
//
//public static final String encodeMap2JsonObject(Map<String, Object> dto) {
//	try {
//		ObjectMapper objectMapper = new ObjectMapper();
//		return objectMapper.writeValueAsString(dto);
//	}
//	catch (IOException e) {
//		e.printStackTrace();
//		return null;
//	}
//}

}
